﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace SalesSys.Comm.Utility
{
    public class ToolEx 
    {
        public static KeyValuePair<TKey, TValue> CreateKV<TKey, TValue>(TKey key, TValue value)
        {
            return new KeyValuePair<TKey, TValue>(key, value);
        }
    }

    public class RegexConst
    {
        /// <summary>
        /// 国内手机
        /// </summary>
        public const string  CellPhone = @"^[1][3-8]\d{9}$";
        /// <summary>
        /// 是否身份证号，非严格验证，验证如下3种情况：
        /// 1.身份证号码为15位数字；
        /// 2.身份证号码为18位数字；
        /// 3.身份证号码为17位数字+1个字母
        /// </summary>
        public const string IdCard= @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$";
        /// <summary>
        /// 6~18位，字母开头
        /// </summary>
        public const string LoginName = @"^[a-zA-Z]\w{5,17}$";
    }

    public static class StringEx
    {
      public static string TrimStart(this string str, string trim)
      {
          if (str.StartsWith(trim))
          {
              return str.Substring(trim.Length-1);
          }
          return str;
      }
      public static string TrimEnd(this string str,string trim)
      {
          if (str.EndsWith(trim))
          {
              return str.Substring(0,str.Length-trim.Length);
          }
          return str;
      }
      public static bool  IsNullOrWhiteSpaceAny(params string[] strs)
      {
          foreach (var item in strs)
          {
              if (string.IsNullOrWhiteSpace(item))
              {
                  return true;
              }
          }
          return false;
      }
        /// <summary>
        /// 指示所指定的正则表达式在指定的输入字符串中是否找到了匹配项
        /// </summary>
        /// <param name="value">要搜索匹配项的字符串</param>
        /// <param name="pattern">要匹配的正则表达式模式</param>
        /// <returns>如果正则表达式找到匹配项，则为 true；否则，为 false</returns>
        public static bool IsMatch(this string value, string pattern)
        {
            if (value == null)
            {
                return false;
            }
            return Regex.IsMatch(value, pattern);
        }

        /// <summary>
        /// 在指定的输入字符串中搜索指定的正则表达式的第一个匹配项
        /// </summary>
        /// <param name="value">要搜索匹配项的字符串</param>
        /// <param name="pattern">要匹配的正则表达式模式</param>
        /// <returns>一个对象，包含有关匹配项的信息</returns>
        public static string Match(this string value, string pattern)
        {
            if (value == null)
            {
                return null;
            }
            return Regex.Match(value, pattern).Value;
        }

        /// <summary>
        /// 在指定的输入字符串中搜索指定的正则表达式的所有匹配项的字符串集合
        /// </summary>
        /// <param name="value"> 要搜索匹配项的字符串 </param>
        /// <param name="pattern"> 要匹配的正则表达式模式 </param>
        /// <returns> 一个集合，包含有关匹配项的字符串值 </returns>
        public static IEnumerable<string> Matches(this string value, string pattern)
        {
            if (value == null)
            {
                return new string[] { };
            }
            MatchCollection matches = Regex.Matches(value, pattern);
            return from Match match in matches select match.Value;
        }

      /// <summary>
      /// 通过分割字符，获取数据数组，默认分隔符为，  只支持常见基本类型
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="str"></param>
      /// <param name="separator"></param>
      /// <returns></returns>
        public static T[] GetArrayBySplit<T>(this string str,params string[] separator) 
        {
            if (str == null)
                return new T[] { };
            if (separator==null||separator.Length<1)
            {
                separator = new[] { "," };
            }
            var s = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            var ty = typeof(T);

            if (ty == typeof(string))
            {
                return s.Cast<T>().ToArray();
            }
            else if (ty == typeof(int))
            {
                return s.Select(p => Convert.ToInt32(p)).Cast<T>().ToArray();
            }
            else if (ty==typeof(float))
            {
                 return s.Select(p => Convert.ToSingle(p)).Cast<T>().ToArray();
            }
            if (ty==typeof(Guid))
            {
                return s.Select(p => Guid.Parse(p)).Cast<T>().ToArray();
            }
            throw new ArgumentException("无法转化的类型");
        }
        public static T ConvertTo<T>(this string str)
        {
            var type = typeof(T);
            return (T)ConvertTo(str, type);
        }
        public static object ConvertTo (this string str,Type type)
        {
            var conv = typeof(IConvertible);
            if (conv.IsAssignableFrom(type))
            {
                return Convert.ChangeType(str, type);
            }
            else if (type == typeof(Guid))
            {
                return Guid.Parse(str);
            }
            else if (type == typeof(byte[]))
            {
                return Convert.FromBase64String(str);
            }
            throw new ArgumentException("无法转化的类型");
        }
        public static T? ToType<T>(string str) where T : struct
        {
            var type = typeof(T);
            return (T?)ToType(str, type);

        }

        private static object ToType(string str, Type type)  
        {
            if (type == typeof(DateTime))
            {
                DateTime dt;
                if (DateTime.TryParse(str, out dt))
                {
                    return  dt;
                }
                else
                {
                    return null;
                }
            }
            else if (type == typeof(int))
            {
                int d;
                if (int.TryParse(str, out d))
                {
                    return  d;
                }
                else
                {
                    return null;
                }

            }
            return null;
        }
    }
  public static class NumEx
  {
      public static  int ToIntOrDefault(string intStr,int def)
      {
          int intTmp = 0;
          if (int.TryParse(intStr, out intTmp))
              return intTmp;
          else
              return def;
      }
      public static int? ToIntOrDefault(string intStr)
      {
          int intTmp = 0;
          if (int.TryParse(intStr, out intTmp))
              return intTmp;
          else
              return null;
      }
  }
}
